/*
* $Id: ChooseForm.java 270 2011-02-20 13:59:40Z volok-aleksej@yandex.ru $
*
* Copyright 2008-2011 by Volokitin Alexey
* All rights reserved
*
*/
package apiary.ui.journal;

import java.util.Vector;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemStateListener;

import apiary.component.journal.Wizard;
import apiary.common.FormHelper;
import apiary.ui.CategoryItem;
import apiary.ui.ItemEventListener;
import javax.microedition.lcdui.Command;

public class ChooseForm extends WizardForm implements ItemStateListener,
                                                      ItemEventListener
{
    public class ChooseValue
    {
        public String value;
        public int id;

        public ChooseValue() {
            value = "";
        }
    }

    private CategoryItem curSel;

    public ChooseForm(String name, Integer id, Wizard wiz, boolean nextCommand) {
        super(name, wiz, id, nextCommand);
        setItemStateListener(this);
    }

    public void setData(Vector data) {
        deleteAll();
//        append(getTitle() + "\n");
        for(int i = 0; i < data.size(); i++) {
            ChooseValue value = (ChooseValue) data.elementAt(i);
            CategoryItem item = FormHelper.createCategoryItem(this,
                    String.valueOf(value.id), value.value, "");
            append(item);
        }
        CheckEmpty();
    }

    public void CheckEmpty() {
        if(size() == 0)
            removeCommand(okCommand);
        else
            addCommand(okCommand);
    }
    public void itemStateChanged(Item item) {
        curSel = (CategoryItem) item;
    }

    public void itemNotifyEvent(Item item, String event) {
        if(event.equals(ItemEventListener.PRESS)){
            curSel = (CategoryItem) item;
            commandOk();
        }
    }
    
    public Object GetDataForm() {
        ChooseValue value = new ChooseValue();
        if(curSel != null) {
            value.id = Integer.parseInt(curSel.getId());
            value.value = curSel.getName();
        }
        return value;
    }
    
    protected void commandUser(Command command) {
    }
}
